﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using HM.UI.HMService;
using GalaSoft.MvvmLight.Command;
using System.Windows.Controls;
using System.Windows;
using HM.UI.Windows;

namespace HM.UI.Converters
{
    class ManageTenantsViewModel : INotifyPropertyChanged
    {
        private HMServiceClient proxy = new HMServiceClient();

        public ManageTenantsViewModel()
        {
            this.RefreshTenants();
            Add = new RelayCommand(AddTenantExecute);
            SaveChanges = new RelayCommand(SaveChangesExecute);
            RemoveSelected = new RelayCommand(RemoveSelectedExeute, CanExecuteRemoveSelected);
        }

        #region Refresh

        private void RefreshTenants()
        {
            this.proxy.GetTenantsCompleted += (s, e) =>
            {
                this.Tenants = e.Result;
            };
            this.proxy.GetTenantsAsync();
        }

        #endregion

        #region Execute

        public RelayCommand Add { get; set; }

        private void AddTenantExecute()
        {
            SaveChangesExecute();

            this.proxy.AddTenantCompleted += (s, e) =>
            {
                RefreshTenants();
            };

            proxy.AddTenantAsync(new Tenant() { IsRenter = false });
        }

        public RelayCommand SaveChanges { get; private set; }

        private void SaveChangesExecute()
        {
            if (this.Tenants != null)
            {
                this.proxy.SaveTenantsCompleted += (s, e) =>
                {
                    this.RefreshTenants();
                };
                this.proxy.SaveTenantsAsync(this.Tenants.ToArray());
            }
        }

        public RelayCommand RemoveSelected { get; private set; }

        private void RemoveSelectedExeute()
        {
            this.proxy.RemoveTenantCompleted += (s, e) =>
            {
                this.RefreshTenants();
            };
            this.proxy.RemoveTenantAsync(this.SelectedTenant);
        }

        #endregion

        #region CanExecute

        private bool CanExecuteRemoveSelected()
        {
            return this.SelectedTenant != null;
        }

        #endregion

        #region Properties

        private IEnumerable<Tenant> tenants;

        public IEnumerable<Tenant> Tenants
        {
            get
            {
                return this.tenants;
            }

            set
            {
                if (tenants != value)
                {
                    this.tenants = value;
                    this.OnPropertyChanged("Tenants");
                }
            }
        }

        private Tenant selectedTenant;

        public Tenant SelectedTenant
        {
            get
            {
                return this.selectedTenant;
            }

            set
            {
                if (selectedTenant != value)
                {
                    this.selectedTenant = value;
                    this.OnPropertyChanged("selectedTenant");
                }
            }
        }

        #endregion

        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

    }
}
